package controller;

import java.util.Scanner;

/**
 * @author Mr.gao
 * @date 2021/4/26 16:56
 **/
public class Gobang {

    private char color ;
    int count = 1;
    /**
     * 棋盘的绘制
     * @return
     */
    public char[][] drawGobang() {
        char[] arrays ={' ','0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
        // 1.自定义二维数组来描述棋盘，默认初始值为0
        char[][] array = new char[16][16];

        // 2.使用双重for循环和数组来实现棋盘的绘制
        for (int i = 0; i < array.length; i++) {
            array[i][0] = arrays[i];
            for (int j = 1; j < array[i].length; j++) {
                if(i == 0 ){
                    array[i][j] = arrays[j];
                }else {
                    array[i][j] = '+';
                }
            }
        }
        return array;
    }

    /**
     * 两人交换下棋的方法
     */
    public void playGobang() {
        char[][] array = drawGobang();
        boolean flag = true;

        int num = 0;
        Scanner scanner = new Scanner(System.in);
        int x =0;
        int y =0;
        while (flag){
            if(num % 2 ==0) {
                System.out.println("请执白棋的人输入落子的横纵坐标");
                 x = scanner.nextInt();
                 y = scanner.nextInt();
            }else {
                System.out.println("请执黑棋的人输入落子的横纵坐标");
                x = scanner.nextInt();
                y = scanner.nextInt();
            }
            if(num % 2 ==0){
                color = '○';
                if(x >0 && x <= 15 && y >0 && y <= 15 ){
                    if(43 == array[x][y] ){
                        array[x][y] = color;
                        show(array);
                        isWinner(x,y,array);
                        if(isWinner(x,y,array) == true){
                            flag = false;
                            continue;
                        }
                    }else {
                        System.out.println("此处已经有棋子，请重新落子");
                        continue;
                    }
                }else {
                    System.out.println("您输入的坐标不符合要求");
                    continue;
                }
            }else {
                color = '●';
                if(x >= 0 && x <= 15 && y >= 0 && y <= 15 ){
                    if(43 == array[x][y]){
                        array[x][y] = color;
                        show(array);
                        isWinner(x,y,array);
                        if(isWinner(x,y,array) == true){
                            flag = false;
                            continue;
                        }
                    }else {
                        System.out.println("此处已经有棋子，请重新落子");
                        continue;
                    }
                }else {
                    System.out.println("您输入的坐标不符合要求");
                    continue;
                }
            }
            num++;
        }
    }

    /**
     * 判断输赢的方法
     * @param i 本次输入的棋子的横坐标
     * @param j  本次输入的棋子的纵坐标
     * @param  array
     * @return
     */
    public boolean isWinner(int i,int j,char[][] array){
        count = 1;
        //横向向右判断
        for (int m = j+1; m < 16; m++) {
            if(array[i][m] == color){
                count++;
            }else {
                break;
            }

        }
        //横向向左判断
        for (int m = j-1; m >= 0 ; m--) {
            if(array[i][m] == color){
                count++;
            }else {
                break;
            }
        }
        if(count >= 5){
            System.out.println(color=='○'?"白棋赢了":"黑棋赢了");
            return true;
        }
        count =1;
        //纵向向上判断
        for (int m = i-1; m >= 0 ; m--) {
            if(array[m][j] == color){
                count++;
            }else {
                break;
            }
        }
        //纵向向下判断
        for (int m = i+1; m < 16 ; m++) {
            if(array[m][j] == color){
                count++;
            }else {
                break;
            }
        }
        if(count >= 5){
            System.out.println(color=='○'?"白棋赢了":"黑棋赢了");
            return true;
        }

        count =1;
        //右上
        for (int m = i-1,n = j+1; m >=0 && n < 16 ; m-- ,n++) {
            if(array[m][n] == color){
                count++;
            }else {
                break;
            }
        }
        //左下
        for (int m = i+1,n = j-1; m < 16 && n >= 0 ; m++ ,n--) {
            if(array[m][n] == color){
                count++;
            }else {
                break;
            }
        }
        if(count >= 5){
            System.out.println(color=='○'?"白棋赢了":"黑棋赢了");
            return true;
        }

        count =1;
        //左上
        for (int m = i-1,n = j-1; m >=0 && n >= 0 ; m-- ,n--) {
            if(array[m][n] == color){
                count++;
            }else {
                break;
            }
        }
        //右下
        for (int m = i+1,n = j+1; m < 16 && n < 16 ; m++ ,n++) {
            if(array[m][n] == color){
                count++;
            }else {
                break;
            }
        }
        if(count >= 5){
            System.out.println(color=='○'?"白棋赢了":"黑棋赢了");
            return true;
        }

        return false;
    }

    /**
     * 绘制的棋盘的打印
     * @param array
     */
    public void show(char[][] array){
        for (int i = 0; i < array.length ; i++) {
            for (int j = 0; j < array[i].length ; j++) {
                System.out.print(array[i][j]+"\t");
            }
            System.out.println();
        }
    }


}
